package dao

import (
	"genealogy/models"
)

func GetAllRelationship(Relationship map[string]interface{}) (error, []models.Relationship, int64) {
	var RelationshipData []models.Relationship
	page := Relationship["page"].(int)
	pageSize := Relationship["limit"].(int)
	searchName := Relationship["searchName"].(string)
	var total int64
	err := db.Table("relationship").Select("person_name, person_no, person_father, person_mother, person_son, person_daughter, person_grandfather, person_grandmother, update_time").Where("person_name like ? and is_del = false", searchName+"%").Order("person_no ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&RelationshipData).Error
	return err, RelationshipData, total
}
func DelRelationship(id string) error {
	err := db.Table("relationship").Where("person_no = ?", id).Update("is_del", true).Error
	return err
}

func UpdateRelationship(Relationship models.Relationship) error {
	err := db.Table("relationship").Where("person_no = ?", Relationship.PersonNo).Updates(&Relationship).Error
	return err
}

func AddRelationship(Relationship models.Relationship) error {
	err := db.Table("relationship").Select("person_name", "person_no", "person_father", "person_mother", "person_son", "person_daughter", "person_grandfather", "person_grandmother", "update_time", "is_del").Create(&Relationship).Error
	return err
}
